package com.shuyuntu.proposal.dao.hib;

import com.shuyuntu.proposal.dao.IProposalDao;
import com.shuyuntu.proposal.dao.base.BaseDaoHib;
import com.shuyuntu.proposal.entity.Department;
import com.shuyuntu.proposal.entity.Proposal;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.*;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 提案对象的数据访问对象的Hibernate实现
 *
 * @author limeng
 * Created by limeng on 16-4-17.
 */
@Repository("proposalDao")
public class ProposalDaoHib extends BaseDaoHib<Proposal, Integer> implements IProposalDao {
    @Override
    public List<Proposal> querySearchPageList(int pageNo, int pageSize, StringBuffer rowCount, Order order, Criterion... criterions) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass);

        criteria.setProjection(Projections.projectionList()
                .add(Projections.property("id"), "id")
                .add(Projections.property("title"), "title")
                .add(Projections.property("author"), "author")
                .add(Projections.property("createTime"), "createTime")
                .add(Projections.property("updateTime"), "updateTime")
                .add(Projections.property("publishTime"), "publishTime")
                .add(Projections.property("proposalState"), "proposalState")
                .add(Projections.property("proposalType"), "proposalType")
                .add(Projections.property("hostDepartment"), "hostDepartment")
                .add(Projections.property("clickCount"), "clickCount")
        ).setResultTransformer(Transformers.aliasToBean(Proposal.class));

        for (Criterion criterion : criterions) {
            if (criterion != null) {
                criteria.add(criterion);
            }
        }
        if (rowCount != null) {
            rowCount.append(queryCount(criterions));
        }
        if (order != null) {
            criteria.addOrder(order);
        }

        List<Proposal> list = criteria.setFirstResult((pageSize * (pageNo - 1))).setMaxResults(pageSize).list();
        return list;
    }

    @Override
    public List<Proposal> queryProposalsByAssistDepartmentId(
            int pageNo, int pageSize, String assistDepartmentId, StringBuffer rowCount) {
        Session session = sessionFactory.getCurrentSession();

        String countHql = "select count(*) from Proposal p " +
                "join p.assistDepartments a where a.id = :assistDepartmentId";
        Query countQuery = session.createQuery(countHql);
        countQuery.setParameter("assistDepartmentId", assistDepartmentId);
        rowCount.append((countQuery.uniqueResult()));

        String hql = "select p from Proposal p " +
                "join p.assistDepartments a where a.id = :assistDepartmentId";
        Query query = session.createQuery(hql);
        query.setParameter("assistDepartmentId", assistDepartmentId);
        query.setFirstResult((pageNo - 1) * pageSize);
        query.setMaxResults(pageSize);

        List<Proposal> list = query.list();
        return list;
    }
}